package com.gzhu.knowledgeAdmin.controller;

import com.alibaba.fastjson.JSONObject;

import com.gzhu.knowledgeAdmin.common.param.UserInfoParam;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.valid.AddGroup;
import com.gzhu.knowledgeAdmin.common.valid.UpdateGroup;
import com.gzhu.knowledgeAdmin.common.vo.MenuVO;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import com.gzhu.knowledgeAdmin.common.vo.RoleVo;
import com.gzhu.knowledgeAdmin.service.RoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.gzhu.knowledgeAdmin.entity.Xtyh;
import com.gzhu.knowledgeAdmin.service.XtyhService;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import java.util.List;

/**
 * 系统用户表
 *
 * @author giaogiao
 * @email giaogiao@gmail.com
 * @date 2023-04-22 23:18:59
 */
@RestController
@RequestMapping("knowledgeAdmin/xtyh")
public class XtyhController {
    @Autowired
    private XtyhService xtyhService;

    @Autowired
    private RoleService roleService;


    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, String uuid)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = xtyhService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable Integer userId) {
        RoleVo roleMap = roleService.findRoleByUserId(userId);
        return R.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('user.assgin')")
    public R doAssign(@RequestParam Integer userId,@RequestParam String roleId) {
        roleService.saveUserRoleRealtionShip(userId,roleId);
        return R.ok();
    }
    /**
     * 获取菜单
     * @return
     */
    @GetMapping("menu")
    public R getMenu(){
        List<MenuVO> permissionList = xtyhService.getCurUserMenu();
        return R.ok(permissionList);
    }

    // 获取用户信息
    @GetMapping("/userInfo")
    public R getUserInfo() {
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = xtyhService.getUserInfo(username);
        return R.ok(userInfo);
    }




    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('user.list')")
    public R list(@RequestParam Map<String, Object> params) {
        PageVo page = xtyhService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('user.update')")
    public R info(@PathVariable("id") Integer id) {
        Xtyh xtyh = xtyhService.getById(id);
        return R.ok(xtyh);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('user.add')")
    public R save(@RequestBody @Validated(AddGroup.class) Xtyh xtyh) {
        xtyhService.saveXtyh(xtyh);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('user.update')")
    public R update(@RequestBody @Validated(UpdateGroup.class) Xtyh xtyh) {

        xtyhService.updateXtyh(xtyh);
        return R.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('user.remove')")
    public R delete(@RequestBody List<Integer> ids) {
        xtyhService.removeXtyhByIds(ids);
        return R.ok();
    }

    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public R logout(){
        xtyhService.logout();
        return R.ok();
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public R resetPwd(@RequestBody List<Integer> ids) {
        xtyhService.resetPwd(ids);
        return R.ok();
    }

    /**
     * 修改个人密码
     */
    @PutMapping("/updatePwd")
    public R updatePwd(@RequestBody UserInfoParam userInfoParam) {
        xtyhService.updatePwd(userInfoParam);
        return R.ok();
    }

    /**
     * 密码是否正确
     */
    @PutMapping("/equlasOldPwd")
    public R equlasOldPwd(@RequestParam String oldPassword) {
        Boolean res = xtyhService.equlasOldPwd(oldPassword);
        return R.ok(res);
    }


    @GetMapping("/nameIsExist/{name}")
    public R nameIsExist(@PathVariable("name")String name){
        boolean res = xtyhService.nameIsExist(name);
        return R.ok(res);
    }
}
